import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:fluwx/fluwx.dart';
import 'package:jtsell_app/config/app_config.dart';

typedef WXCodeCallback = void Function(String wxCode);

class FluwxUtils {
  static FluwxUtils _instance;

  FluwxUtils() {
    fluwx.register(appId: AppConfig.WX_APP_KEY, doOnAndroid: true, doOnIOS: true, enableMTA: false);
  }

  static FluwxUtils getInstance() {
    if (_instance == null) {
      _instance = new FluwxUtils();
    }

    return _instance;
  }

  ///
  /// 微信授权监听
  ///
  void wechatAuthListen(WXCodeCallback wxCodeCallback) {
    fluwx.responseFromAuth.listen((WeChatAuthResponse data) {
      if (data.code != null && data.code.isNotEmpty) {
        print("wechat code ---->${data.code}");

        wxCodeCallback(data.code);
      }
    });
  }

  ///
  /// 开始微信登录操作
  ///
  void wechatLogin() {
    fluwx.sendAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test").then((data) {
      print("wechatLogin---->$data");
    });
  }
}
